define(['app', 'json5', 'clipboard'], function(app, JSON5, Clipboard) {
    app.controller('TestMockController', function($http, $timeout, $scope, xContext, $uibModalInstance) {
        var self = this;
        self.url = '';
        self.method = 'GET';
        self.response = '';
        self.parsable = true;
        self.completed = true;

        var host = null;
        var body = null;
        var jsonEditor = null;

        self.init = function() {
            self.url = xContext.host + xContext.path;

            new Clipboard('#copy-response');

            var maxHeight = 150;
            jsonEditor = ace.edit('json-request');
            jsonEditor.getSession().setMode("ace/mode/hjson");
            jsonEditor.setOptions({
                fontSize: 12,
                showGutter: false,
                showLineNumbers: false,
                maxPixelHeight: maxHeight,
                showPrintMargin: false,
                autoScrollEditorIntoView: true,
                enableBasicAutocompletion: true,
                enableSnippets: true,
                enableLiveAutocompletion: true
            });
            jsonEditor.setValue('/*支持JSON的对象格式与注释*/\n');
            jsonEditor.on('change', function(e) {
                var newValue = jsonEditor.getValue();
                self.parsable = true;
                if (newValue !== '') {
                    try {
                        body = JSON5.parse(newValue);
                    } catch (ex) {
                        self.parsable = false;
                    }
                }
                $timeout(function() { $scope.$apply(); });
            });
            jQuery('#json-request, #json-response').css('font-size', '12px').height(maxHeight);
        }

        self.send = function(valid) {
            self.response = '';
            if (valid) {
                if (new RegExp(xContext.path + '$').test(self.url)) {
                    host = self.url.substring(0, self.url.lastIndexOf(xContext.path));
                } else {
                    var matches = self.url.match(/^http(s)?:\/\/[\w-\.]+(:\d+)?/);
                    if (matches != null && matches.length > 0) {
                        host = matches[0];
                    }
                }
                if (self.parsable) {
                    var data = null;
                    if (self.method == 'POST' && body != null) {
                        data = body;
                    }
                    self.completed = false;
                    $http({
                        url: '/mock/data/test',
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json;charset=UTF-8'
                        },
                        timeout: 10000,
                        data: {
                            url: self.url,
                            method: self.method,
                            data: data
                        }
                    }).then(function(response) {
                        self.completed = true;
                        if (response.headers('Content-Type').indexOf('application/json') > -1) {
                            self.response = JSON.stringify(response.data, null, '    ');
                        } else {
                            self.response = response.data;
                        }
                    }, function(response) {
                        self.completed = true;
                    });
                }
            }
        }

        self.close = function() {
            $uibModalInstance.close({ host: host });
        }
    });
});
